<template id="food">
    <div>
        <el-input style="width: 25%;" v-model="spmc" placeholder="请输入名称"></el-input>
        &nbsp;&nbsp;
        <el-button style="width: 15%;" @click="findAll" type="primary">查询</el-button>
        <el-button style="width: 15%;" @click="kk=true"  type="primary">添加</el-button>
        <el-table :data="list">
        <el-table-column prop="id" label="编号" style="display: none;"> 
        </el-table-column>
        <el-table-column prop="bm" label="编码" width="140">
        </el-table-column>
        <el-table-column prop="spmc" label="食品名称" width="120">
        </el-table-column>
        <el-table-column prop="type" label="类型">
        </el-table-column>
        <el-table-column prop="foodenergy" label="食品能量">
        </el-table-column>
        <el-table-column prop="foodprotein" label="食品蛋白质">
        </el-table-column>
        <el-table-column prop="foodfat" label="食品脂肪">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary"  icon="el-icon-edit" @click="queryId(scope.row.id),up=true"  circle></el-button>
            <el-popconfirm @confirm="del(scope.row.id)" title="这是一段内容确定删除吗">
            <el-button type="danger" slot="reference" icon="el-icon-delete" circle></el-button>
          </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[3, 5, 7, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
<!--添加-->
    <el-dialog
    title="添加"
    :visible.sync="kk"
    width="30%"
    center>
    <el-form :model="food" status-icon  ref="food" label-width="100px" class="demo-ruleForm">
    食品名称:<el-input v-model="food.spmc" style="width: 80%;"/><br>
    <h1></h1>
    食品类别:<el-select v-model="food.type" style="width: 80%;">
      <el-option label="" value="大豆类"></el-option>
      <el-option label="" value="谷薯类"></el-option>
      <el-option label="" value="蔬菜类"></el-option>
      <el-option label="" value="水果类"></el-option>
      <el-option label="" value="肉类"></el-option>
      <el-option label="" value="奶类"></el-option>
      <el-option label="" value="蛋类"></el-option>
    </el-select><br>
    <h1></h1>
    食品能量:<el-input v-model="food.foodenergy" style="width: 80%;"/><br>
    <h1></h1>
    食品蛋白质:<el-input v-model="food.foodprotein" style="width: 80%;"></el-input><br>
    <h1></h1>
    食品脂肪:<el-input v-model="food.foodfat" style="width: 80%;"></el-input><br>
    <h1></h1>
    食品说明:<el-input v-model="food.explain" style="width: 80%;"></el-input><br>
    <h1></h1>
  
    <el-button @click="reset('food'),kk = false">取 消</el-button>
    <el-button type="primary" @click="kk = false,add()" >确 定</el-button>
</el-form>
</el-dialog>

<!-- 修改 -->
<el-dialog
    title="修改"
    :visible.sync="up"
    width="30%"
    center>
    <el-form :model="foodup" status-icon  ref="food" label-width="100px" class="demo-ruleForm">
      <input type="hidden" v-model="foodup.id"/>
    食品名称:<el-input v-model="foodup.spmc" style="width: 80%;"/><br>
    <h1></h1>
    食品类别:<el-select v-model="foodup.type" style="width: 80%;">
      <el-option label="" value="大豆类"></el-option>
      <el-option label="" value="谷薯类"></el-option>
      <el-option label="" value="蔬菜类"></el-option>
      <el-option label="" value="水果类"></el-option>
      <el-option label="" value="肉类"></el-option>
      <el-option label="" value="奶类"></el-option>
      <el-option label="" value="蛋类"></el-option>
    </el-select><br>
    <h1></h1>
    食品能量:<el-input v-model="foodup.foodenergy" style="width: 80%;"/><br>
    <h1></h1>
    食品蛋白质:<el-input v-model="foodup.foodprotein" style="width: 80%;"></el-input><br>
    <h1></h1>
    食品脂肪:<el-input v-model="foodup.foodfat" style="width: 80%;"></el-input><br>
    <h1></h1>
    食品说明:<el-input v-model="foodup.expl" style="width: 80%;"></el-input><br>
    <h1></h1>
  
    <el-button @click="reset(),up = false">取 消</el-button>
    <el-button type="primary" @click="up = false,update()" >修改</el-button>
</el-form>
</el-dialog>
    </div>
</template>

<script>
export default {
    data(){
        return{
          up:false,
          kk:false,
            total:10,
            pageSize:4,
            currentPage4:1,
            spmc:"",
            list:[],
            food:{spmc:"",type:"",foodenergy:"",foodprotein:"",foodfat:"",explain:""},
            foodup:{id:"",spmc:"",type:"",foodenergy:"",foodprotein:"",foodfat:"",expl:""},
        }
    },
    methods:{
        findAll(){
          this.axios.post("/tomcat/Z_queryAll",{page:this.currentPage4,num:this.pageSize,spmc:this.spmc})
          .then(s=>{
            this.list=s.data.rows
            this.total=s.data.total
          })
        },
        del(id){
            this.axios.get("/tomcat/Z_del?id="+id).then(s=>{
                  if(s.data>0){
                    this.$notify({
                      title: '成功',
                      message: '这是一条删除成功的提示消息',
                      position: 'top-left',
                      type: 'success'
                    });
                  }
                  this.findAll()
            })
        },
        update(){
          this.axios.post("/tomcat/Z_up",{id:this.foodup.id,spmc:this.foodup.spmc,type:this.foodup.type,
            foodenergy:this.foodup.foodenergy,foodprotein:this.foodup.foodprotein,
            foodfat:this.foodup.foodfat,expl:this.foodup.expl}).then(s=>{
              if(s.data>0){
              this.$notify({
                title: '成功',
                message: '这是一条成功的提示消息',
                type: 'success'
              });
              this.findAll();
            }
          })
        },
        add(){
          this.axios.post("/tomcat/Z_add",{spmc:this.food.spmc,type:this.food.type,foodenergy:this.food.foodenergy,foodprotein:this.food.foodprotein,
           foodfat:this.food.foodfat,expl:this.food.explain
          }).then(s=>{
            if(s.data>0){
              this.$notify({
                title: '成功',
                message: '这是一条成功的提示消息',
                type: 'success'
              });
              this.findAll();
              this.reset();
            }
          })
        },
        queryId(id){
          this.axios.post("/tomcat/Z_queryId",{id:id}).then(s=>{
            console.log(this.foodup=s.data)
              this.foodup=s.data
          })
        },
        handleSizeChange(val) {
            this.pageSize=val
            this.findAll();
        
        },
        handleCurrentChange(val) {
            this.currentPage4=val
            this.findAll();
        },
        reset(){
          console.log(this.food)
           this.food={}
        }
    },
    created(){
        this.findAll();
    }
}
</script>

<style>

</style>